#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "sans.h"
#include "garbagecollector.h"
#include "runtime.h"
#include "symboltable.h"

sdata bltn_number_question(sdata x) {
  return SDBOOL(x.tag == tnumb);
}

sdata bltn_null_question(sdata x) {
    return SDBOOL(x.tag == tnull);
}

sdata bltn_bool_question(sdata x) {
    return SDBOOL(x.tag == tbool);
}

sdata bltn_string_question(sdata x) {
    return SDBOOL(x.tag == tstrn);
}

sdata bltn_procedure_question(sdata x) {
    return SDBOOL(x.tag == tclos);
}

sdata bltn_vector_question(sdata x) {
    return SDBOOL(x.tag == tvect);
}

sdata bltn_pair_question(sdata x) {
    return SDBOOL(x.tag == tcons);
}

sdata bltn_sym_sym_question(sdata x) {
    return SDBOOL(x.tag == tsymb);
}

sdata bltn_char_question(sdata x) {
    return SDBOOL(x.tag == tchar);
}

sdata bltn_eof_object_question(sdata x) {
    return SDBOOL(x.tag == teof);
}

sdata bltn_char__tointeger(sdata ob) {
  if (ob.tag == tchar) {
    return SDNUMB(ob.data.ivalue);
  }
  else {
    log_err("runtime/bltn_char__tointeger: not a number\n");
  }
}
sdata bltn_integer__tochar(sdata ob) {
  if (ob.tag == tnumb) {
    return SDCHAR(ob.data.ivalue);
  }
  else {
    log_err("runtime/bltn_integer__tochar: not a number\n");
  }
}

sdata bltn_sym_sym__tolist(sdata ob) {
  sdata memo = SDNULL();
  char* str;
  int i;
  if (ob.tag == tsymb) {
    str = lookup(ob.data.value);
    for(i=strlen(str); i > 0; i--) {
      memo = cons(SDCHAR(str[i-1]), memo);
    }
    return memo;
  }
  else {
    log_err("runtime/bltn_symbol__tolist: not a symbol\n");
  }
}

